<?php

namespace App\Http\Service;

use App\Libs\HttpCurlLibrary;
use Illuminate\Support\Facades\Log;
use App\Services\Ip2RegionService;
use Illuminate\Support\Facades\DB;
use App\Http\Service\UserService;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cache;

#获取佣金 补贴 
class MoneyService extends BaseService
{
    private $ip2RegionService;
    public function __construct(UserService $userService)
    {
        parent::__construct();
        $this->userService=$userService;
        $this->platformRateType1 = $this->getPlatformAllRate(1);
        $this->platformRateType2 = $this->getPlatformAllRate(2);
        #自用商品详情
        $this->personalDetail=config('myurl.personal_detail');
        echo "333333;";

    }
    #获取平台比例 type=1 爆料  type=2 心动日记
    public function getPlatformAllRate($type=1)
    {
        if(empty($type)){
            $type=1;
        }
        if($type==1){
            $data=platform_rate();
        }else if($type==2){
            $data=platform_xdrj_rate();
        }
        

        return $data;
    }
    public function getPlatformRate($type=1)
    {
        if(empty($type)){
            $type=1;
        }
        if($type==1){
            $data=$this->platformRateType1;
        }else if($type==2){
            $data=$this->platformRateType2;
       }
       echo "222222;";
       return $data;
    }

    #获取用户信息
    public function getUserDate($people_id)
    {
        $data=$this->userService->update_user($people_id);
        return $data;
    }

    #获取用户等级比例
    public function userLevelRate($levelId)
    {
        $data=user_level_rate($levelId);
        return $data;
    }
    #获取用户实时总佣金比例
    public  function get_total_commission_ing($unit_price,$subsidy_rate,$commission_rate,$platform_name,$people_id,$is_rate_true=1,$is_owner=true,$type=1,$update_time="",$product_id="")
    {
        $deal_update_time=date("Y-m-d",strtotime($update_time));
        $now_time=date("Y-m-d");
        if($update_time && $product_id){
            #update_time 和 product_id 和 people_id 不为空
            if($deal_update_time==$now_time){
                $result=$this->estimatedTotalCommission($unit_price,$subsidy_rate,$commission_rate,$platform_name,$people_id,$is_rate_true,$is_owner,$type);

            }else{
               
                $getinfo_url=$this->personalDetail['url'];
                $user_params=[
                    "product_id"=>$product_id,
                    "platform"=> $platform_name,
                    "user_id"=> $people_id

                ];
                $txt_data=[
                    'subsidy_rate'=>0.00,
                    'commission_rate'=>0.00,
                    'product_id'=>$product_id,
                    'platform_name'=>$platform_name,
                    'time'=>date("Y-m-d")
                ];
                $pp=$product_id.$txt_data['platform_name'].$txt_data['time'];
                $cacheKey = 'baoliao:' . md5($pp);
                // 尝试从缓存获取
                $cachedResult = cache()->get($cacheKey);
                if ($cachedResult !== null) {
                    $subsidy_rate=$cachedResult['subsidy_rate'];
                    $commission_rate=$cachedResult['commission_rate'];
                    $result=$this->estimatedTotalCommission($unit_price,$subsidy_rate,$commission_rate,$platform_name,$people_id,$is_rate_true,$is_owner,$type);

                    return $result;
                }
                $postdata=json_post($getinfo_url,$user_params);
                $result=[
                    'subsidy_amount'=>0.00,
                    'commission_amount'=>0.00,
                    'total_commission'=>0.00,
                ];

               
                if(isset($postdata['status'])&&$postdata['status']='200'){
                    if(isset($postdata['data'])){
                        $userdata=$postdata['data'];
                        //dd($userdata);
                        #佣金
                        if(isset($userdata['commission'])){
                            $result['commission_amount']=$userdata['commission'];
                        }
                        #佣金占比
                        if(isset($userdata['commission_rate'])){
                            $txt_data['commission_rate']=$userdata['commission_rate'];
                        }

                        if(isset($userdata['platform_subsidy'])){
                            $platform_subsidy=$userdata['platform_subsidy'];
                            if(isset($platform_subsidy[0])){
                                $platform_subsidy=$platform_subsidy[0];
                                if(isset($platform_subsidy['subsidy_amount'])){
                                    $result['subsidy_amount']=$platform_subsidy['subsidy_amount'];
                                }
                                //dd($platform_subsidy['subsidy_rate']);
                                #补贴比例
                                if(isset($platform_subsidy['subsidy_rate'])){
                                     $txt_data['subsidy_rate']=$platform_subsidy['subsidy_rate'];
                                }
                            }
                        }

                    }

                }



            }
            
            // 写入缓存
            cache()->put($cacheKey, $txt_data, 3600);
            //dd($cacheKey);
            $result['total_commission']=$result['commission_amount']+$result['subsidy_amount'];
        }else{
            #为空
            $result=$this->estimatedTotalCommission($unit_price,$subsidy_rate,$commission_rate,$platform_name,$people_id,$is_rate_true,$is_owner,$type);
        }

        return $result;

    }
    


/*    预估佣金=预计付款金额x佣金比例x是否自营x平台预留比例x用户等级比例


    补贴佣金=预计付款金额 x补贴比例x是否自营x平台预留比例x用户等级比例


    实际佣金=实际付款金额x佣金比例x是否自营x平台预留比例x用户等级比例


    补贴佣金=实际付款金额 x补贴比例x是否自营x平台预留比例x用户等级比例*/

     /**
     * 获取 补贴金额(subsidy_amount)=下单价(unit_price)*补贴比例(subsidy_rate)*平台比例(platform_rate)*用户等级比例(user_level_rate)
     */
     #获取平台比例 type=1 爆料  type=2 心动日记
    public function subsidyAmount($unit_price,$subsidy_rate,$platform_name,$people_id,$is_rate_true=1,$is_owner=true,$type=1)
    {
        if(empty($type)){
            $type=1;
        }
        
        if($is_rate_true){
            $subsidy_rate=$subsidy_rate/100;
        }

        $platform_rate_arr=$this->getPlatformRate($type);
        $platform_rate=$platform_rate_arr[$platform_name];
        if($type==2){
            $levelId=3;
        }else if($type==1){
            $getUserDate_arr=$this->getUserDate($people_id);
            $levelId=$getUserDate_arr['levelId'];
        }
        
        
        
        $user_level_rate=$this->userLevelRate($levelId);


        // 在计算补贴金额之前，先输出各个变量的值

        //dd($user_level_rate);
        $subsidy_amount=$unit_price*$subsidy_rate*$platform_rate*$user_level_rate;



        // 简洁版本
        //echo "DEBUG: unit_price=$unit_price, subsidy_rate=$subsidy_rate, platform_rate=$platform_rate, user_level_rate=$user_level_rate, result=" . ($unit_price * $subsidy_rate * $platform_rate * $user_level_rate) . "\n";

        //dd($subsidy_amount);
        if(!$is_owner && $platform_name=='京东' ){
            $subsidy_amount = $subsidy_amount*0.9;
        }
        //dd($subsidy_amount);
        return $subsidy_amount;
     
    }


    /**
     * 获取 佣金金额(commission_amount)=下单价(unit_price)*佣金比例(commission_rate)*平台比例(platform_rate)*用户等级比例(user_level_rate)
     */
        
    public  function commissionAmount($unit_price,$commission_rate,$platform_name,$people_id,$is_rate_true=1,$is_owner=true,$type=1)
    {
        if(empty($type)){
            $type=1;
        }
        
        if($is_rate_true){
            $commission_rate=$commission_rate/100;
        }

       
        $platform_rate_arr=$this->getPlatformRate($type);
        $platform_rate=$platform_rate_arr[$platform_name];
        if($type==1){
            $getUserDate_arr=$this->getUserDate($people_id);
            //dd($getUserDate_arr);
            $levelId=$getUserDate_arr['levelId'];
        }else if($type==2){
            $levelId=3;
        }
        
        
        $user_level_rate=$this->userLevelRate($levelId);

        $commission_amount=$unit_price*$commission_rate*$platform_rate*$user_level_rate;
        // 简洁版本打印佣金金额计算
/*                                */
        //dd($is_owner);
        // 京东非自营打 90%
        if(!$is_owner && $platform_name=='京东' ){
            $commission_amount = $commission_amount*0.9;


        }
        //dd($commission_rate);
        return $commission_amount;
    }

    /**
     * 
     * 获取 总佣金金额(commission_amount)=下单价(unit_price)*佣金比例(commission_rate)*平台比例(platform_rate)*用户等级比例(user_level_rate)
     */
    public function estimatedTotalCommission($unit_price,$subsidy_rate,$commission_rate,$platform_name,$people_id,$is_rate_true=1,$is_owner=true,$type=1)
    {
        if(empty($type)){
            $type=1;
        }
        $subsidyAmount=$this->subsidyAmount($unit_price,$subsidy_rate,$platform_name,$people_id,$is_rate_true,$is_owner=true,$type);
       // dd($subsidyAmount);

        $commissionAmount=$this->commissionAmount($unit_price,$commission_rate,$platform_name,$people_id,$is_rate_true,$is_owner=true,$type);
        //$commissionAmount=$this->estimated_amount($unit_price,$commission_rate,$platform_name,$people_id,$is_rate_true=1,$is_owner=true);

        $total_commission=$subsidyAmount+$commissionAmount;
        $result=[
            'subsidy_amount'=>$subsidyAmount,
            'commission_amount'=>$commissionAmount,
            'total_commission'=>$total_commission,
        ];
        //dd($commissionAmount);
        return $result;

    }
    #获取佣金金额 京东自营 commission_amount=$unit_price*$commission_rate
    #获取佣金金额 京东非自营 commission_amount=$unit_price*$commission_rate*0.9
    #获取佣金金额 is_owner=true (京东自营)  is_rate_true 占比是否除以100  是否是京东自营
    # 表里边存的
    public function commissionAmountTable($unit_price,$commission_rate,$platform_name,$quantity=1,$is_rate_true=1,$is_owner=true)
    {
        if($is_rate_true){
            $commission_rate=$commission_rate/100;
        }

        $commission_amount=$unit_price*$commission_rate;
         // 京东非自营打 90%
        if(!$is_owner && $platform_name=='京东' ){
            $commission_amount = $commission_amount*0.9;

        }
        return $commission_amount;
    }

    #预估佣金=预计付款金额x佣金比例x是否自营x平台预留比例x用户等级比例

    public function estimated_amount($unit_price,$commission_rate,$platform_name,$people_id,$is_rate_true=1,$is_owner=true)
    {
        if($is_rate_true){
            $commission_rate=$commission_rate/100;

        }

        //dd($commission_rate);
       
        $platform_rate_arr=$this->getPlatformRate();
        $platform_rate=$platform_rate_arr[$platform_name];
        //dd($platform_rate);
        $getUserDate_arr=$this->getUserDate($people_id);
        //dd($getUserDate_arr);
        $levelId=$getUserDate_arr['levelId'];
        $user_level_rate=$this->userLevelRate($levelId);
        $estimated_amount=$unit_price*$commission_rate*$platform_rate*$user_level_rate;
        //dd($is_owner);
        // 京东非自营打 90%
        if(!$is_owner && $platform_name=='京东' ){
            $estimated_amount = $estimated_amount*0.9;


        }
        return $estimated_amount;
    }


    public function purchase_steps($purchase_steps,$quantity,$title,$platform_name,$commission_amount,$unit_price)
    {
        //下单价=单价*数量
       // dd($unit_price);
        $final_price=$unit_price*$quantity;
         //佣金总价
        $commission_amount_total= $commission_amount*$quantity;
        //dd($purchase_steps);
        foreach ($purchase_steps as $key => &$val) {
           /* #加购
            if($val['type']==3){
                dd($val);
            }*/
            #下单
            if($val['type']==5){
                  //下单
                if($quantity>1){

                    $lastunitPrice = ($unit_price-$commission_amount_total)/$quantity;
                    $lastunitPrice=_round($lastunitPrice);
                    $commission_amount_total=_round($commission_amount_total);
                    $val['description']="下单{$quantity}件实付{$unit_price}，下单后返利约{$commission_amount_total}元，单价合{$lastunitPrice}元";
                }else{
                    $commission_amount_total=_round($commission_amount_total);
                    $val['description']="实付{$final_price}，下单后返利约{$commission_amount_total}元";
                }
            }
        }
        
        //dd($purchase_steps);

        return $purchase_steps;




    }


}
